package de.steric.battlemon.collision;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.util.vector.Vector3f;

import de.steric.battlemon.GameObject;
import de.steric.battlemon.GameObject;

public class CollisionOverGroup extends collisionGroup {
	private ArrayList<collisionGroup> children;

	public CollisionOverGroup() {

	}

	public boolean addItem(GameObject newItem) {
		boolean isOK = false;
		for (collisionGroup element : children) {
			isOK = element.addItem(newItem);
			if (isOK) {
				break;
			}
		}
		if (!isOK) {
			children.add(new collisionObjectGroup(newItem));
			isOK = true;
		}
		return isOK;
	}

	@Override
	public void createGroupCollider() {
		sort();
		// size in X Direction
		float maxX = this.children.get(0).groupCollider.getboundingVerticeAt(0).x;
		float minX = this.children.get(0).groupCollider.getboundingVerticeAt(0).x;
		// size in Y Direction
		float maxY = this.children.get(0).groupCollider.getboundingVerticeAt(0).y;
		float minY = this.children.get(0).groupCollider.getboundingVerticeAt(0).y;

		// size in Z Direction
		float maxZ = this.children.get(0).groupCollider.getboundingVerticeAt(0).z;
		float minZ = this.children.get(0).groupCollider.getboundingVerticeAt(0).z;

		for (collisionGroup element : children) {
			for (int i = 0; i < 8; i++) {
				// X
				maxX = Math.max(maxX,
						element.groupCollider.getboundingVerticeAt(i).x);
				minX = Math.min(minX,
						element.groupCollider.getboundingVerticeAt(i).x);

				// Y
				maxY = Math.max(maxY,
						element.groupCollider.getboundingVerticeAt(i).y);
				minY = Math.min(minY,
						element.groupCollider.getboundingVerticeAt(i).y);

				// Z
				maxZ = Math.max(maxZ,
						element.groupCollider.getboundingVerticeAt(i).z);
				minZ = Math.min(minZ,
						element.groupCollider.getboundingVerticeAt(i).z);
			}
		}

		this.groupCollider.setboundingVerticeAt(0, new Vector3f(minX, minY,
				minZ));
		this.groupCollider.setboundingVerticeAt(1, new Vector3f(maxX, minY,
				minZ));
		this.groupCollider.setboundingVerticeAt(2, new Vector3f(maxX, minY,
				maxZ));
		this.groupCollider.setboundingVerticeAt(3, new Vector3f(minX, minY,
				maxZ));
		this.groupCollider.setboundingVerticeAt(4, new Vector3f(minX, maxY,
				minZ));
		this.groupCollider.setboundingVerticeAt(5, new Vector3f(maxX, maxY,
				minZ));
		this.groupCollider.setboundingVerticeAt(6, new Vector3f(maxX, maxY,
				maxZ));
		this.groupCollider.setboundingVerticeAt(7, new Vector3f(minX, maxY,
				maxZ));
	}

	void sort() {
		for (collisionGroup element : children) {
			if (element.getClass() == CollisionOverGroup.class) {
				((CollisionOverGroup) element).sort();
			} else {
				if (((collisionObjectGroup) element).isEmpty()) {
					children.remove(element);
				}
			}
		}
	}

	@Override
	ArrayList<GameObject> isCollision(GameObject other) {
		ArrayList<GameObject> collisions = new ArrayList<GameObject>();
		for (collisionGroup element : children) {
			collisions.addAll(element.isCollision(other));
		}
		return collisions;
	}

}
